<?php
namespace app\index\controller;

use think\Request;
use \app\admin\model\Designer as DesignerModel;

class Designer extends Base
{
    protected $model;
    
    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $this->model = new DesignerModel();
    }

    public function index($id)
    {
        // 正文
        $data = $this->model->find($id);

        $data->clicks +=1;
        
        @$data->save();

        $banner = $this->category_model->where('id', $data->cid)->value('banner');

        $data->banner = $banner;

        // 产品图册
        $pictures = new \app\admin\model\Pictures();

        $data_pic = $pictures->field('id,thumb,image')
            ->where(['cid'=>$data->cid, 'sid'=>$id, 'status'=>1])
            ->select();

        // 其他设计师
        $minId = $this->model->min('id');
        $maxId = $this->model->max('id');
        if ($id == $minId) {
            $data['prev'] = $this->model->field('id, name, photograph')->find($maxId);
        } else {
            $data['prev'] = $this->model->field('id, name, photograph')
                ->where('id', '<', $id)
                ->whereOr('id', $minId)
                ->order('id desc')
                ->limit('1')
                ->find();
        }
        if ($id == $maxId) {
            $data['next'] = $this->model->field('id, name, photograph')->find($minId);
        } else {
            $data['next'] = $this->model->field('id, name, photograph')
                ->where('id', '>', $id)
                ->whereOr('id', $maxId)
                ->order('id asc')
                ->limit('1')
                ->find();
        }

        $this->assign('data', $data);
        $this->assign('data_pic', $data_pic);
        return $this->fetch();
    }

    public function lists($cid = 61)
    {
        // 后面可能会变数组,先输出当前cid
        $this->assign('cid', $cid);

        // banner,img_title图
        $data_banner =  $this->category_model->where('id', $cid)->field('banner, img_title')->find();

        if ($cid == 61) {
            $cid = model('category')->where('pid', $cid)->column('id');
        }
        $data = $this->model->where('cid', 'in', $cid)
            ->where('status', 1)
            ->order('top')
            ->paginate(6);

        // 设计师分类
        $position = $this->category_model->where('pid', 61)->column('id, name');

        $this->assign('data', $data);
        $this->assign('data_banner', $data_banner);
        $this->assign('position', $position);
        return $this->fetch();
    }
}
